package com.medata.utils;

import com.medata.exeptions.YunExceptions;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ：zb
 * @date ：Created in 2019/8/20 0020 11:22
 * @description：
 */

public class PreUtils {
    public static void prePronunciations(StringRedisTemplate redisTemplate, String solrPath){
        HashOperations<String, Object, Object> ops = redisTemplate.opsForHash();
        HttpSolrServer solrServer = new HttpSolrServer(solrPath + "words");
        for (char i = 'a'; i <= 'z'; i++) {
            String searchKey = String.valueOf(i);
            int j = 0;
//            List<String> list=new ArrayList<>();
            Map<String, String> map = new HashMap<>();
            while (j < 500000) {
                SolrQuery query = new SolrQuery();
                query.set("q", searchKey + "*");
                query.set("df", "pinyin2");
//                query.setSort("ID",SolrQuery.ORDER.asc);
                query.setStart(j);
                query.setRows(100 + j);
                query.set("fl", "pinyin,word,pinyin2");
                try {
                    QueryResponse response = solrServer.query(query);
                    SolrDocumentList results = response.getResults();
                    if (results != null && !results.isEmpty()) {
                        System.out.println(i);
                        for (SolrDocument document : results) {
                            Object word = document.get("word");
                            if (word != null && ((String) word).length() == 1) {
                                if (((String)word).equals("拗")){
                                    System.out.println("a");
                                }
                                System.out.println("b");
                                Object pronunciation = document.get("pinyin2");
                                if (pronunciation != null && ((String) pronunciation).length() > 0) {
//                                    list.add((String)word+":"+(String)pronunciation);
                                    String s = map.get((String) pronunciation);
                                    Object pinyin = document.get("pinyin");
                                    if (s != null) {
                                        if (pinyin != null && ((String) pinyin).length() > 0) {
                                            String kv=(String)word+(String)pinyin;
                                            if (!s.contains((String) word + ":" + (String) pinyin)) {
                                                s = s + "@@@" + (String) word + ":" + (String) pinyin;
                                                map.put((String) pronunciation, s);
                                            }
                                        }
                                    } else {
                                        if (pinyin != null && ((String) pinyin).length() > 0) {
                                            map.put((String) pronunciation, (String) word + ":" + (String) pinyin);
                                        }
                                    }
                                }
                            }
                        }
                    } else {
                        break;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new YunExceptions(0, "保存首字母到redis时出现bug");
                }
                j = j + 100;
            }
            ops.put("pronunciation", searchKey, map.toString());
        }
    }
}
